// mediasoup 配置文件
export const config = {
  // mediasoup Worker 设置
  worker: {
    rtcMinPort: 10000,
    rtcMaxPort: 12000, // 扩展端口范围支持更多并发连接
    logLevel: 'warn',
    logTags: [
      'info',
      'ice',
      'dtls',
      'rtp',
      'srtp',
      'rtcp',
      'rtx',
      'bwe',
      'score',
      'simulcast',
      'svc'
    ]
  },

  // mediasoup Router 设置
  router: {
    mediaCodecs: [
      {
        kind: 'audio',
        mimeType: 'audio/opus',
        clockRate: 48000,
        channels: 2
      },
      {
        kind: 'video',
        mimeType: 'video/VP8',
        clockRate: 90000,
        parameters: {
          'x-google-start-bitrate': 1000
        }
      },
      {
        kind: 'video',
        mimeType: 'video/VP9',
        clockRate: 90000,
        parameters: {
          'profile-id': 2,
          'x-google-start-bitrate': 1000
        }
      },
      {
        kind: 'video',
        mimeType: 'video/h264',
        clockRate: 90000,
        parameters: {
          'packetization-mode': 1,
          'profile-level-id': '4d0032',
          'level-asymmetry-allowed': 1,
          'x-google-start-bitrate': 1000
        }
      },
      {
        kind: 'video',
        mimeType: 'video/H264',
        clockRate: 90000,
        parameters: {
          'packetization-mode': 1,
          'profile-level-id': '42e01f',
          'level-asymmetry-allowed': 1,
          'x-google-start-bitrate': 1000
        }
      }
    ]
  },

  // WebRTC Transport 设置
  webRtcTransport: {
    listenIps: [
      {
        ip: '0.0.0.0',
        announcedIp: '113.44.10.21' // 本地测试使用localhost
      }
    ],
    maxIncomingBitrate: 1500000,
    initialAvailableOutgoingBitrate: 1000000,
    enableUdp: true,
    enableTcp: true,
    preferUdp: true
  },

  // Plain Transport 设置 (用于接收 RTSP 流)
  plainTransport: {
    listenIp: {
      ip: '0.0.0.0',
      announcedIp: '113.44.10.21'
    },
    rtcpMux: false,
    comedia: true
  },

  // RTSP 流设置
  rtsp: {
    //rtsp://admin:TuWen666666@192.168.5.64:554/Streaming/Channels/1
    // 默认 RTSP 流 URL (可以通过 API 动态设置)
    defaultUrl: 'rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4',
    // FFmpeg 参数
    ffmpegOptions: {
      video: {
        codec: 'libvpx',
        bitrate: '1000k',
        fps: 25,
        width: 640,
        height: 480
      },
      audio: {
        codec: 'libopus',
        bitrate: '128k',
        sampleRate: 48000,
        channels: 2
      }
    }
  },

  // HTTP 服务器设置
  http: {
    port: 3000,
    host: '0.0.0.0'
  },
  // HTTP 服务器设置
  https: {
    port: 443,
    host: '0.0.0.0'
  },

  // Socket.IO 设置
  socketIO: {
    cors: {
      origin: "*",
      methods: ["GET", "POST"]
    }
  }
};

// 获取本机 IP 地址
import { networkInterfaces } from 'os';

export function getLocalIP() {
  const nets = networkInterfaces();
  const results = {};

  for (const name of Object.keys(nets)) {
    for (const net of nets[name]) {
      // 跳过非 IPv4 和内部地址
      if (net.family === 'IPv4' && !net.internal) {
        if (!results[name]) {
          results[name] = [];
        }
        results[name].push(net.address);
      }
    }
  }

  // 返回第一个找到的外部 IP
  const interfaces = Object.values(results);
  if (interfaces.length > 0 && interfaces[0].length > 0) {
    return interfaces[0][0];
  }
  
  return '113.44.10.21';
}

// 更新配置中的 announcedIp (本地测试不需要)
export function updateAnnouncedIp() {
  // 本地测试使用 113.44.10.21，不需要自动检测
  console.log(`Using localhost for local testing`);
  return '113.44.10.21';
}
